// https://leetcode.cn/problems/flood-fill/
// Created by ade on 2022/8/15.
//
#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    int row = 0;
    int col = 0;
    vector <vector<int>> vi;

    vector <vector<int>> floodFill(vector <vector<int>> &image, int sr, int sc, int color) {
        row = image.size();
        col = image[0].size();
        vi.resize(row, vector<int>(col));
        dfs(image, sr, sc, color, image[sr][sc]);
        return image;
    }

    void dfs(vector <vector<int>> &image, int sr, int sc, int color, int beforeColor) {
        if (sr < 0 || sr >= row || sc < 0 || sc >= col
            || image[sr][sc] != beforeColor
            || vi[sr][sc] == 1
                )
            return;
        image[sr][sc] = color;
        vi[sr][sc] = 1;
        dfs(image, sr + 1, sc, color, beforeColor);
        dfs(image, sr - 1, sc, color, beforeColor);
        dfs(image, sr, sc + 1, color, beforeColor);
        dfs(image, sr, sc - 1, color, beforeColor);
    }
};

int main() {
    Solution so;
    vector <vector<int>> nums = {{0, 0, 0},
                                 {0, 0, 0}};
    auto res = so.floodFill(nums, 0, 0, 0);
    for (auto items: res) {
        for (auto i : items) {
            cout << i << ",";
        }
        cout << endl;
    }

    return 0;
}